const db = require('../config/db')
    // 验证邮箱方法引入
const emailFn = require('../dao/emailServe')
const { numFn } = require('../model/Numcode')
    // redis引入
const redisClient = require('../redis/index')
    // 统一返回前端方法
const { send } = require('../model/send')
    // 密码加密、解密方法
const { encryption, verifcation } = require('../dao/bcrypt')
    // token方法
const { generateToken, verifyToken } = require('../dao/jwt')

let Keys = null

const login = (req, res) => {
        let { userInof, password } = req.body
        db.query('SELECT * FROM user ', async(err, er) => {
            if (err) return send(res, 500, 'serveError' + err)
                // 判断数据库账号密码
            const obj = await er.find(item => item.user_email == userInof)

            if (!obj) return res.send({ code: 300, msg: '账号不存在' })
            const token = generateToken(obj.id)
            const passShow = await verifcation(password, obj.user_passwored)

            delete obj.user_passwored
            const data = { token, data: obj }
            passShow ? send(res, 200, '登陆成功', data) : send(res, 300, '密码错误')
        })
    }
    // 验证邮箱
const VerifyMailbox = (req, res) => {
    console.log(req.body.userInof);
    let userInof = req.body.userInof
    db.query('SELECT * FROM user', async(err, er) => {
        if (err) return send(res, 500, '服务器错误' + err)
        const some = er.some(item => item.user_email == userInof)
        if (!some) {
            // 随机验证码
            const num = numFn()
                /* 增 改*/
            Keys = num
            setTimeout(() => {
                    Keys = null
                }, 60000)
                // await redisClient.set('key', num) // 设置值
                // redisClient.expire('key', 60); //60秒自动过期
                // 发送邮箱
            emailFn(userInof, res, num)
        } else {
            send(res, 300, '该邮箱已绑定已存在')
        }

    })
}


// 注册
const Register = async(req, res) => {
    let { userInof, password, nickName, verifynum } = req.body
    console.log(req.body);
    // 加密密码
    password = encryption(password)
    if (!userInof || !password || !nickName) return res.send({ code: 401, msg: '请填写账号密码和昵称' })
        // const value = await redisClient.get('key') // 得到value 没有则为null
    if (verifynum.toLowerCase() != Keys || Keys == null || Keys == undefined) return res.send({ code: 300, msg: '验证码无效，请重新发送' })

    db.query('SELECT * FROM user ', (err, er) => {
        if (err) return send(res, 500, 'serveError' + err)

        index = er.some(item => item.user_email == userInof)
        if (!index) {
            //插入数据
            db.query('insert into user(user_email,user_passwored,user_name,user_age,user_gender,user_personalizedsignature,user_mobilephonenumber,user_img) value(?,?,?,?,?,?,?,?)', [userInof, password, nickName, '未填写', '未填写', '未填写', '未填写', "https://guitar-img-1307419551.cos.ap-nanjing.myqcloud.com/1.jpg"])
            send(res, 200, '注册成功')
        } else {
            send(res, 300, '该账号已存在')
        }
    })
}
module.exports = { login, VerifyMailbox, Register }